//
//  EnvironmentObjectyExampleView.swift
//  TanTan_Kotlin
//
//  Created by EDY on 5/26/25.
//

import SwiftUICore
import SwiftUI

class GlobalCounterModel: ObservableObject {
    @Published var counter = 0

}


struct EnvironmentObjectyExampleView :View {
    @StateObject private var model = GlobalCounterModel()
    var body: some View {
        VStack {
            Text("计数器: \(model.counter)")
                .font(.largeTitle)
            Button(action:{
                model.counter += 1
            }){
                Text("增加")
                    .font(.title)
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
               
            }
            Divider()
            EnvironmentChildView()
                .environmentObject(model)
        }
    }
}


struct EnvironmentChildView: View {
    @EnvironmentObject var model: GlobalCounterModel
    var body: some View {
        Text("环境对象计数器: \(model.counter)")
            .font(.title)
    }
}


#Preview {
    EnvironmentObjectyExampleView()
}
